<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px">
        <div v-show="showForm == 2">
            <div class="container">
                <h3 style="margin-bottom: 20px">产品信息</h3>
                <el-form-item label="产品类型：">
                    {{ruleForm.materialType}}
                </el-form-item>
                <el-row class="demo-ruleForm">
                    <el-col :span="12">
                        <el-form-item label="品名：">
                            {{ruleForm.breedName}}
                        </el-form-item>
                        <el-form-item label="存货代码：">
                            {{ruleForm.inventoryCode}}
                        </el-form-item>
                        <el-form-item label="生产批号：">
                            {{ruleForm.batch}}
                        </el-form-item>
                        <el-form-item label="实际产量：">
                            {{ruleForm.totalNumber}}{{ruleForm.units}}
                        </el-form-item>
                        <el-form-item label="仓库：">
                            {{ruleForm.warehouse}}
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item>
                            &nbsp;&nbsp;
                        </el-form-item>
                        <el-form-item>
                            &nbsp;&nbsp;
                        </el-form-item>
                        <el-form-item label="发药机编码：">
                            {{ruleForm.dispenserCode}}
                        </el-form-item>
                        <el-form-item label="生产负责人：">
                            {{ruleForm.principalUser}}
                        </el-form-item>
                    </el-col>
                </el-row>
            </div>
            <div class="container" style="margin-top:20px">
                <h3 style="margin-bottom: 20px">投料信息</h3>
                <el-form-item label="投料：">
                    {{productName}}
                    &nbsp;&nbsp;
                    {{productBatch}}
                    &nbsp;&nbsp;
                    {{ruleForm.scUserNum}}
                </el-form-item>
            </div>
            <el-form-item class="footerFixed">
                <el-button type="primary" @click="submitForm()">确认无误</el-button>
                <el-button @click="showForm = 1">返回修改</el-button>
            </el-form-item>
        </div>
        <div v-show="showForm == 1">
            <div class="container">
                <h3 style="margin-bottom: 20px">产品信息</h3>
                <el-form-item label="产品类型：" prop="materialType">
                    <el-radio-group v-model="ruleForm.materialType" @change="changeMaterType">
                        <el-radio label="配方颗粒" border>配方颗粒</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-row class="demo-ruleForm">
                    <el-col>
                        <el-form-item label="品名：" prop="breedName">
                            <el-select v-model="ruleForm.breedName" @change="changeBreed" value-key="id" filterable placeholder="请选择" style="width: auto;">
                                <el-option
                                    v-for="item in breedList"
                                    :key="item.id"
                                    :label="item.breedName"
                                    :value="item"
                                >
                                    <span style="float: left">{{ item.breedName }}</span>
                                    <span style="float: right; color: #8492a6; font-size: 13px">{{ item.specUnits }}</span>
                                </el-option>
                            </el-select>
                            <el-button style="margin-left:10px" @click="wuliaoLink">物料管理</el-button>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="存货代码：" prop="inventoryCode">
                            <el-select v-model="ruleForm.inventoryCode" @change="changeInvent" value-key="id" filterable placeholder="请选择" style="width: auto;">
                                <el-option
                                    v-for="item in inventList"
                                    :key="item.id"
                                    :label="item.inventoryCode"
                                    :value="item"
                                >
                                {{ item.inventoryCode }}
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="生产批号：" prop="batch">
                            <el-input v-model="ruleForm.batch" style="width: auto"></el-input>
                        </el-form-item>
                        <el-form-item label="实际产量：" prop="totalNumber">
                            <el-input placeholder="请输入" v-model="ruleForm.totalNumber" style="width: 200px">
                                <template slot="append">
                                    <el-select v-model="ruleForm.units" :disabled="unitDis" placeholder="单位" style="width:80px !important">
                                        <el-option v-for="item in unitList" :key="item.id" :label="item.name" :value="item.name">{{item.name}}</el-option>
                                    </el-select>
                                </template>
                            </el-input>
                            <el-button style="margin-left:10px" @click="unitLink">单位管理</el-button>
                        </el-form-item>
                        <el-form-item label="仓库：">
                            <el-select v-model="ruleForm.warehouse" @change="changeWare" filterable value-key="id" placeholder="请选择" style="width: auto;">
                                <el-option
                                    v-for="item in warehouseList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item"
                                >
                                {{item.name}}
                                </el-option>
                            </el-select>
                            <el-button style="margin-left:10px" @click="cangLink">仓库管理</el-button>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="发药机编码：">
                            {{ruleForm.dispenserCode}}
                        </el-form-item>
                        <el-form-item label="生产负责人：" prop="principalUser">
                            <el-select v-model="ruleForm.principalUser" filterable placeholder="请选择" value-key="id" @change="changePrincipa" style="width: auto">
                                <el-option
                                    v-for="item in userList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item"
                                >
                                {{item.name}}
                                </el-option>
                            </el-select>
                            <el-button style="margin-left:10px" @click="staffsLink">员工管理</el-button>
                        </el-form-item>
                        <el-form-item label="" v-if="ruleForm.materialType != '颗粒包装'">
                            &nbsp;&nbsp;
                        </el-form-item>
                        <el-form-item>
                            &nbsp;&nbsp;
                        </el-form-item>
                        <el-form-item>
                            &nbsp;&nbsp;
                        </el-form-item>
                    </el-col>
                </el-row>
            </div>
            <div class="container" style="margin-top:20px">
                <h3 style="margin-bottom: 20px">投料品信息</h3>
                <el-form-item label="投料" prop="name">
                    <el-select v-model="productName" filterable value-key="id" @change="changePin" placeholder="名称" style="width:200px">
                        <el-option
                            v-for="item1 in breedList"
                            :key="item1"
                            :label="item1"
                            :value="item1"
                        >
                        {{item1}}
                        </el-option>
                    </el-select>
                    <el-select v-model="ruleForm.manufactureId" filterable value-key="id" :disabled="isDisabled" placeholder="生产批号" @change="changeInBatch" style="width:200px;margin: 0 15px">
                        <el-option
                            v-for="item1 in inBatchs"
                            :key="item1.id"
                            :label="item1.inBatch"
                            :value="item1"
                        >
                        {{item1.inBatch}}
                        </el-option>
                    </el-select>
                    <el-input v-model.number="ruleForm.scUserNum" type="number" placeholder="投入量" @change="changeNum" style="width:200px"></el-input>
                </el-form-item>
            </div>
            <el-form-item class="footerFixed">
                <el-button type="primary" @click="submitRules('ruleForm')">提交</el-button>
                <el-button @click="backPage">返回</el-button>
            </el-form-item>
        </div>
        </el-form>
    </div>
</template>

<script>
import {manufacture_findByAllMaterType,user_findAll,unit_findByList,material_findBatchBreedName,manufacture_save,manufacture_findById,warehouse_findAll,manufacture_update,pack_save,print_findByBreedName} from "@/request/api";
    export default {
        components: {
        },
        data() {
            return {
                showForm: 1,
                breedList: [],
                materialType: '',
                userList: [],
                inventList: [],
                unitList: [],
                warehouseList: [],
                isDisabled: true,
                inBatchs: [],
                productName: '',
                productBatch: '',
                ruleForm: {
                    materialType: '配方颗粒',
                    breedName: '',
                    time: '',
                    // directive: '',
                    principalUser: '',
                    principalUserId: '',
                    inventoryCode: '',
                    batch: '',
                    totalNumber: '',
                    units: '',
                    warehouseId: '',
                    warehouse: '',
                    dispenserCode: '',
                    manufactureId: '',
                    type: '投料',
                    breedName: '',
                    inBatch: '',
                    scUserNum: 0,
                    surplusNum: '',
                },
                rules:{
                    materialType: [
                        { required: true, message: '不能为空', trigger: 'change' }
                    ],
                    breedName: [
                        { required: true, message: '不能为空', trigger: 'change' },
                    ],
                    inventoryCode: [
                        { required: true, message: '不能为空', trigger: 'change' }
                    ],
                    totalNumber: [
                        { required: true, message: '不能为空', trigger: 'blur' },
                    ],
                    technique:[
                        { required: true, message: '不能为空', trigger: 'change' }
                    ],
                    principalUser: [
                        { required: true, message: '不能为空', trigger: 'change' }
                    ],
                    batch: [
                        { required: true, message: '不能为空', trigger: 'blur' },
                    ]
                },
                unitDis: false,
                materialTypeStor: '',
                sceneUrls: [],
                productUrls: [],
            };
        },
        mounted(){
            // if(this.$route.query.type == '提取饮片'){
            //     this.ruleForm.materialType = '提取饮片'
            //     this.materialType = '提取饮片'
            // } else
            if(this.$route.query.type == '配方颗粒'){
                this.ruleForm.materialType = '配方颗粒'
                this.materialType = '配方颗粒'
            }
            // else if(this.$route.query.type == '颗粒包装'){
            //     this.ruleForm.materialType = '颗粒包装'
            //     this.materialType = '颗粒包装'
            // }
            this.id = this.$route.query.id
            if(this.id == undefined){
                this.init()
                // if(this.$route.query.type == '提取饮片'){
                //     this.materialTypeStor = '提取饮片'
                // } else
                if(this.$route.query.type == '配方颗粒'){
                    this.materialTypeStor = '配方颗粒'
                }
                // else if(this.$route.query.type == '颗粒包装'){
                //     this.materialTypeStor = '颗粒包装'
                // }
            } else {
                this.init1()
            }
            var ruleForm = this.$store.state.newStorge
            // if (ruleForm.materialType == '提取饮片'){
            //     this.ruleForm = this.$store.state.newStorge
            // } else
            if (ruleForm.materialType == '配方颗粒'){
                this.ruleForm = this.$store.state.newStorge
            }
            // else if (ruleForm.materialType == '颗粒包装'){
            //     this.ruleForm = this.$store.state.newStorge
            // }
        },
        methods: {
            backPage () {
                this.$router.go(-1);
                this.$store.commit('setStorName',this.materialTypeStor)
            },
            // 选择物料类型
            changeMaterType(val){
                // if (val == '提取饮片') {
                //     this.materialType = val
                //     this.init()
                // } else if (val == '配方颗粒') {
                //     this.materialType = val
                //     this.init()
                // }
                // else if (val == '颗粒包装') {
                //     this.materialType = '配方颗粒'
                //     this.init()
                // }
            },
            init(){
                manufacture_findByAllMaterType({
                    materialType: '配方颗粒',
                }).then((res)=>{
                    this.breedList = res.data
                })
                user_findAll('').then((res)=>{
                    this.userList = res.data
                })
                unit_findByList('').then((res) =>{
                    this.unitList = res.data
                })
                // 仓库
                warehouse_findAll('').then((res) =>{
                    this.warehouseList = res.data
                })
            },
            init1(){
                manufacture_findById(this.id).then((res) =>{
                    // if (res.data.materialType == '提取饮片') {
                    //     this.materialType = '提取饮片'
                    //     this.init()
                    // } else
                    if (res.data.materialType == '配方颗粒') {
                        this.materialType = '配方颗粒'
                        this.init()
                    }
                    // else if (res.data.materialType == '颗粒包装') {
                    //     this.materialType = '配方颗粒'
                    //     this.init()
                    // }
                    this.ruleForm = Object.assign({},res.data);
                    this.materialTypeStor = res.data.materialType
                    
                })
            },
            // 选择负责人
            changePrincipa(ele){
                this.ruleForm.principalUserId = ele.id
                this.ruleForm.principalUser = ele.name
            },
            // 选择品名
            changeBreed(ele){
                // console.log(ele)
                this.ruleForm.breedId = ele.id
                this.ruleForm.breedName = ele.breedName
                print_findByBreedName({breedName:ele.breedName}).then((res) =>{
                    console.log(res)
                    this.inventList = res.data
                    // this.ruleForm.loadingQuantity = res.data.loadingQuantity
                    this.ruleForm.dispenserCode = res.data
                })
            },
            wuliaoLink(){
                
                this.$router.push({
                    path:'/materialsManagement/added'
                })
                if (this.ruleForm.materialType=='中药材') {
                    this.$store.commit('setStorge',this.ruleForm)
                } else if (this.ruleForm.materialType=='辅料') {
                    this.$store.commit('setStorge',this.ruleForm)
                } else if (this.ruleForm.materialType=='提取饮片') {
                    this.$store.commit('setStorge',this.ruleForm)
                } else if (this.ruleForm.materialType=='配方颗粒') {
                    this.$store.commit('setStorge',this.ruleForm)
                }
            },
            cangLink(){
                this.$router.push({
                    path:'/inventory/addInventory'
                })
                if (this.ruleForm.materialType=='中药材') {
                    this.$store.commit('setStorge',this.ruleForm)
                } else if (this.ruleForm.materialType=='辅料') {
                    this.$store.commit('setStorge',this.ruleForm)
                } else if (this.ruleForm.materialType=='提取饮片') {
                    this.$store.commit('setStorge',this.ruleForm)
                } else if (this.ruleForm.materialType=='配方颗粒') {
                    this.$store.commit('setStorge',this.ruleForm)
                }
            },
            staffsLink(){
               var data = this.$store.getters.permission
                var items = ""
                for (let i = 0; i < data.length; i++) {
                    if(data[i].name == "员工管理"){
                        items = 1
                    }     
                }
                if (items == "1"){
                    this.$router.push({
                        path:'/staffsManagement/addedstaff'
                    })
                    if (this.ruleForm.materialType=='中药材') {
                        this.$store.commit('setStorge',this.ruleForm)
                    } else if (this.ruleForm.materialType=='辅料') {
                        this.$store.commit('setStorge',this.ruleForm)
                    } else if (this.ruleForm.materialType=='提取饮片') {
                        this.$store.commit('setStorge',this.ruleForm)
                    } else if (this.ruleForm.materialType=='配方颗粒') {
                        this.$store.commit('setStorge',this.ruleForm)
                    }
                }else{
                    this.$router.push({
                        path:'/403'
                    })
                }
            },
            unitLink(){
                this.$router.push({
                    path:'/unit'
                })
                if (this.ruleForm.materialType=='中药材') {
                    this.$store.commit('setStorge',this.ruleForm)
                } else if (this.ruleForm.materialType=='辅料') {
                    this.$store.commit('setStorge',this.ruleForm)
                } else if (this.ruleForm.materialType=='提取饮片') {
                    this.$store.commit('setStorge',this.ruleForm)
                } else if (this.ruleForm.materialType=='配方颗粒') {
                    this.$store.commit('setStorge',this.ruleForm)
                }
            },
            changeWare(ele){
                this.ruleForm.warehouseId = ele.id
                this.ruleForm.warehouse = ele.name
            },
            changeNum(){
                if (this.ruleForm.scUserNum <= 0) {
                    this.$notify.error({
                        title: '提示',
                        message: '数量不能为零或负数！',
                        showClose: false,
                        duration:2000
                    });
                    this.ruleForm.scUserNum = this.ruleForm.surplusNum
                }
                if (this.ruleForm.scUserNum > this.ruleForm.surplusNum) {
                    this.$notify.error({
                        title: '提示',
                        message: '数量不能大于剩余量！',
                        showClose: false,
                        duration:2000
                    });
                    this.ruleForm.scUserNum = this.ruleForm.surplusNum
                }
            },
            changePin() {
                this.isDisabled = false
                const productName = this.productName
                material_findBatchBreedName({
                    breedName: productName
                }).then((res) =>{
                    this.inBatchs = res.data
                })
            },
            changeInBatch(val){
                this.productBatch = val.batch
                this.scUserNum = val.num
            },
            changeInvent(){

            },
            submitRules(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        var breedName = '';
                        var inBatch = '';
                        var scUserNum = '';
                            breedName = this.ruleForm.breedName;
                            inBatch = this.ruleForm.inBatch;
                            scUserNum = this.ruleForm.scUserNum;

                        if(this.ruleForm.units != ''){
                            if(breedName != ''){
                                if (inBatch == '') {
                                    this.$notify.error({
                                        title: '提示',
                                        message: '投料入库批次必填！',
                                        showClose: false,
                                        duration:2000
                                    });
                                } else if(inBatch != ''){
                                    if(scUserNum == ''){
                                        this.$notify.error({
                                            title: '提示',
                                            message: '投料投入量必填！',
                                            showClose: false,
                                            duration:2000
                                        });
                                    } else if(scUserNum <= 0){
                                        this.$notify.error({
                                            title: '提示',
                                            message: '投料投入量不能为零或负数！',
                                            showClose: false,
                                            duration:2000
                                        });
                                    } else{
                                        this.showForm = 2
                                    }
                                }
                            }
                        } else{
                            this.$notify.error({
                                title: '提示',
                                message: '产量单位必填！',
                                showClose: false,
                                duration:2000
                            });
                            this.showForm = 1
                        }
                    } else {
                        return false;
                    }
                    // console.log(this.ruleForm,222)
                })
            },
            submitForm() {
                console.log(this.ruleForm,112)
                // const lists = JSON.stringify(this.ruleForm.list)
                // this.ruleForm.ylString = lists.toString();
                // if (this.ruleForm.flList[0].breedName != '') {
                //     const flLists = JSON.stringify(this.ruleForm.flList)
                //     this.ruleForm.flString = flLists.toString();
                // } else{
                //     this.ruleForm.flString = [];
                // }

                // if (this.ruleForm.flList[0].breedName == '') {
                //     this.ruleForm.flList = []
                // }
                // var formdata = new FormData()
                // push到formdata
                // for(var a in this.ruleForm){
                //     formdata.append(a,this.ruleForm[a])
                // }
                // console.log(this.ruleForm,11111)

                if(this.id == undefined){
                    // pack_save(this.ruleForm).then((res) => {
                    //     this.$notify({
                    //         title: '提示',
                    //         message: '保存成功',
                    //         type: 'success',
                    //         duration:1000
                    //     });
                    //     this.backPage()
                    // })
                } else{
                    // console.log(this.ruleForm,2222)
                    pack_update(this.ruleForm).then((res) => {
                        this.$notify({
                            title: '提示',
                            message: '修改成功',
                            type: 'success',
                            duration:1000
                        });
                        this.backPage()
                    })
                }
            },
        }
    }
</script>
<style scoped lang="less">
.el-date-editor.el-input{
    width: 200px !important
}
.el-select{
    width: 200px !important
}
.demo-ruleForm{
    margin-top: 20px;
}
</style>